#!/bin/sh

# Slackware build script for scilab 

# Copyright (c) 2009 João Felipe Santos
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# The .desktop file was retrieved from the ArchLinux User Repository, 
# where it is mantained by StefanHusmann

PRGNAM=scilab			
VERSION=${VERSION:-4.1.2}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}	
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e 

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION-src.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --with-tk \
  --with-gfortran \
  --build=$ARCH-slackware-linux

make CC_OPTIONS+="$SLKCFLAGS" FC_OPTIONS+="$SLKCFLAGS" all
make PREFIX=$PKG/usr DESTDIR=$PKG install

# We've got a lot of embedded $TMP and $PKG paths in the package, so...
sed -i "s%$PKG%/usr/lib%g" $PKG/usr/bin/scilab	# Do this one first
for i in $(grep -R '/tmp/SBo' $PKG | cut -d: -f1) ; do
  sed -i "s%$PKG%%g" $i 
  sed -i "s%$TMP%%g" $i
done
# Now let's fix up a couple of symlinks
( cd $PKG/usr/bin
  rm -f intersci intersci-n
  ln -sf ../lib/$PRGNAM-$VERSION/bin/intersci intersci
  ln -sf ../lib/$PRGNAM-$VERSION/bin/intersci-n intersci-n
)

# Add a desktop menu entry
mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/scilab.desktop > $PKG/usr/share/applications/scilab.desktop
( cd $PKG/usr/share/pixmaps
  ln -s ../../lib/$PRGNAM-$VERSION/X11_defaults/scilab.xpm .
)

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null
)

mv $PKG/usr/share/doc $PKG/usr
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
( cd $PKG/usr/doc/$PRGNAM-$VERSION
  for i in demos examples man ; 
    do ln -s ../../lib/$PRGNAM-$VERSION/$i . ;
  done
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
